package test1;

import java.io.FileInputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Security;
import java.security.Signature;
import java.security.SignatureException;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;

public class encrypt {
	public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
		PublicKey publicKey = null;
		try{

			//生成文件输入流,输入文件为c:/mycert.cer
      		FileInputStream fin = new FileInputStream("400121000023.cer");

      		//获取一个处理X.509证书的证书工厂
      		CertificateFactory certFactory = CertificateFactory.getInstance("X.509");

      		//获取证书
      		Certificate mycert = certFactory.generateCertificate(fin);

      		fin.close();
      		ReadPFX r=new ReadPFX();
      		publicKey = mycert.getPublicKey();
      		System.out.println(r.byte2HexStr(publicKey.getEncoded()));
      }catch (Exception ex){ex.printStackTrace();}
		
		
		
		ReadPFX r = new ReadPFX();
		PrivateKey pfx = r.GetPvkformPfx("nxy.pfx", "111111");
		System.out.println(r.Byte2String(pfx.getEncoded()));
		Signature signature=Signature.getInstance("MD5withRSA");
		signature.initSign(pfx);
		signature.update("abcdefghsdasdas".getBytes());
		byte[] sign = signature.sign();
		
		Signature signature2=Signature.getInstance("MD5withRSA");
		signature2.initVerify(publicKey);
		signature2.update("abcdefghsdasdas".getBytes());
		boolean verify = signature2.verify(sign);
		System.out.println(verify);
		
	}

}
